package com.cskaoyan.utils;

import java.util.Random;

/**
 * @ClassName CharUtil
 * @Description TODO 生成随机数 → 注册验证码
 * @Author by Lester
 * @Date 2022/11/23 11:46
 * @Version 1.0
 */
public class CharUtil {

    //                               生成num位的随机字符串
    public static String getRandomString(Integer num) {
        String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        // 随机数生成器
        Random random = new Random();
        // 可变字符串类，用来追加字符串
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num; i++) {
            // 生成随机下标
            int index = random.nextInt(base.length());
            // 在sb中追加字符
            sb.append(base.charAt(index));
        }
        return sb.toString();
    }

    //                       随机生成num位的纯数字验证码
    public static String getRandomNum(Integer num) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num; i++) {
            int index = random.nextInt(base.length());
            sb.append(base.charAt(index));
        }
        return sb.toString();
    }
}
